package com.github.iot.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.github.iot.R;
import com.github.iot.bean.SendLightBean;
import com.github.iot.entity.Light;
import com.github.iot.utils.BusUtils;
import com.github.iot.view.indicatorseekbar.IndicatorSeekBar;
import com.github.iot.view.indicatorseekbar.OnSeekChangeListener;
import com.github.iot.view.indicatorseekbar.SeekParams;

import java.util.List;

/**
 * 灯列表适配器
 *
 * @Author: 梁声洪
 * @Date: 2019/12/24 22:47
 * @Version: v1.0.0
 */
public class LightAdapter extends RecyclerView.Adapter<LightAdapter.LightViewHolder> {

    private Context context;
    private List<Light> list;
    private View inflater;

    public LightAdapter(Context context, List<Light> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public LightViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(context).inflate(R.layout.light_item, parent, false);
        LightViewHolder viewHolder = new LightViewHolder(inflater);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(LightViewHolder holder, int position) {
        Light light = list.get(position);
        holder.mName.setText(light.getName());
        holder.mLight.setProgress(light.getProgress());

        holder.mLight.setOnSeekChangeListener(new OnSeekChangeListener() {
            @Override
            public void onSeeking(SeekParams seekParams) {
                try {
                    BusUtils.post(new SendLightBean(light, seekParams.progress));
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStartTrackingTouch(IndicatorSeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(IndicatorSeekBar seekBar) {

            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class LightViewHolder extends RecyclerView.ViewHolder {
        AppCompatTextView mName;
        IndicatorSeekBar mLight;

        public LightViewHolder(View itemView) {
            super(itemView);
            mName = itemView.findViewById(R.id.tv_light_name);
            mLight = itemView.findViewById(R.id.bar_light);
        }
    }

}